package ext.tianma.bom.service.sqe;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import ext.com.core.CoreUtil;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.util.WTException;

public class SQEPartService0 {

	public static String getShowData(String numType, String number) throws WTException {

		number = number.replace("，", ",");
		String[] nums = number.trim().split(",");
		String num = "";
		String str = "";
		String Estr = "";
		System.out.println("-----nums------" + nums.toString());

		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		JSONArray Earray = new JSONArray();
		JSONObject Eobject = new JSONObject();

		try {
			if ("project".equals(numType)) {

				for (int i = 0; i < nums.length; i++) {
					num = nums[i];
					ArrayList<WTPart> partList = SQEPartHelper.searchIBA("projectName", num);
					if (partList.size() == 0) {
						// String estr = "编号有误，请重新输入!";
						// JSONObject obj = new JSONObject();
						// obj.put("estr", estr);
						Earray.put(nums[i]);
						Eobject.put("status", "failed");
						Eobject.put("datalist", Earray);
						Estr = Eobject.toString();
					} else {
						for (int j = 0; j < partList.size(); j++) {
							WTPart p = partList.get(j);
							List<WTPart> pList = SQEPartHelper.getAllLatestVersionPart(p.getNumber());
							array = SQEPartHelper.setData(pList, array);
							object.put("status", "success");
							object.put("datalist", array);
							str = object.toString();
							// object.append("datalist", obj);
						}
					}
				}

			} else if ("part".equals(numType)) {

				for (int i = 0; i < nums.length; i++) {
					num = nums[i];
					System.out.println("-----partNum-----" + num);
					WTPartMaster temp = CoreUtil.getWTPartMasterByNumber(num);
					if (temp == null) {
						// String estr = "编号有误，请重新输入!";
						// JSONObject obj = new JSONObject();
						// obj.put("estr", estr);
						System.out.println("failed");
						Earray.put(nums[i]);
						Eobject.put("status", "failed");
						Eobject.put("datalist", Earray);
						Estr = Eobject.toString();
					} else {
						System.out.println("success");
						List<WTPart> partList = SQEPartHelper.getAllLatestVersionPart(num);
						array = SQEPartHelper.setData(partList, array);
						object.put("status", "success");
						object.put("datalist", array);
						str = object.toString();
					}
				}

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (!"".equals(Estr)) {
			System.out.println("Estr");
			return Estr;
		} else {
			System.out.println("str");
			return str;
		}

	}
}
